package com.clstu.file;

import org.testng.annotations.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy {
    public static void main(String[] args) {

    }

    @Test
    public void copyFile() throws FileNotFoundException {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            fileOutputStream= new FileOutputStream("D:\\wuwu.pdf");
            fileInputStream = new FileInputStream("D:\\haha.pdf");

            int readLen = 0;//读取字节数
            byte[] buf = new byte[1024];//缓冲区

            while ((readLen=fileInputStream.read(buf))!=-1){
                fileOutputStream.write(buf,0,readLen);//写入（注意这里一定只能用这个方法，带上长度，不然可能会多写入一些错误的数据）
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fileInputStream!=null) fileInputStream.close();//最后即时关闭文件访问
                if (fileOutputStream!=null) fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
